<template>
	<view>
		<view style="background: #efefef;padding:30rpx;">
			<u--text v-if="myOrder" text="当前会员等级:"></u--text>
			<u-row v-if="myOrder" customStyle="border-radius:10rpx;padding:15rpx;">
				<u-col span="3">
					<view customStyle="width: 150rpx;">
						<u-tag :text="listT[myOrder.order_type].name" type="warning" plain plainFill></u-tag>
					</view>
				</u-col>
				<u-col span="3" offset="1">
					<u--text text="截止时间:"></u--text>
				</u-col>
				<u-col span="4">
					<u--text mode="date" :text="userInfo.vip_end_time" size="12" color="#878787"></u--text>
				</u-col>
			</u-row>
		</view>
		<view style="padding: 30rpx 25rpx;">
			<!-- <u-tabs :list="listT" @click="click"></u-tabs> -->

			<u-radio-group v-model="radiovalue1" @change="groupChange" placement="column">
				<u-row v-for="(item, index) in package" :key="index">
					<u-col span="12">
						<u-row>
							<u-col span="4" customStyle="align-items: center;">
								<u--text :text="listT[item.vip_type].name" color="#1E88E6" size="16"></u--text>
							</u-col>
							<u-col span="3" customStyle="align-items: center;">
								<u--text customStyle="margin:0 auto" :text="item.sale_price + '元'" color="#E67B1E" bold>
								</u--text>
								<u--text customStyle="margin:0 auto;text-decoration: line-through;"
									:text="item.scribing_price + '元'" size="12" decoration="line-through"
									color="#7a7a7a">
								</u--text>
							</u-col>
							<u-col span="3" customStyle="align-items: center;">
								<view>
									<u--text customStyle="margin:0 auto" text="有效期" color="#040200"></u--text>
									<u--text customStyle="margin:0 auto" v-if="item.validity_term_day"
										:text="item.validity_term_day + '天'" color="#040200" size="12"></u--text>
									<u--text customStyle="margin:0 auto" v-else-if="item.validity_time_month"
										:text="item.validity_time_month + '月'" color="#040200" size="12"></u--text>
									<u--text customStyle="margin:0 auto" v-else="item.validity_time_year"
										:text="item.validity_time_year + '年'" color="#040200" size="12"></u--text>
								</view>
							</u-col>
							<u-col span="3">
								<u-radio :customStyle="{ marginBottom: '8px' }" :name="index" @change="radioChange">
								</u-radio>
							</u-col>
						</u-row>
						<u-line customStyle="margin: 16rpx 0;"></u-line>
					</u-col>
				</u-row>
			</u-radio-group>

			<u--text customStyle="margin-top: 20rpx;" text="特权说明" color="#ff0000" bold size="15"></u--text>
			<u--text v-for="(item, index) in description[chooseVip.vip_type]" customStyle="margin-top: 20rpx;"
				:text="item" color="#1E88E6" bold size="15"></u--text>
			<u--text customStyle="margin-top: 20rpx;" text="特权说明" color="#ff0000" bold size="15"></u--text>

			<u--text customStyle="margin: 20rpx 0 180rpx 0;"
				text="【提醒】: 所有数据均存放在云端,数据安全有保证,手机丢了录入数据不会丢;会员等级只能往上升,不能往回降.升级等级时,剩余有效期按1/3折算进新版本有效期" color="#7a7a7a"
				size="15"></u--text>

			<view
				style=" border: 2rpx solid rgba(0, 0, 0, 0.2);padding: 10rpx;box-shadow: 6rpx 8rpx 26rpx -6rpx rgba(0, 0, 0, 0.2);position: fixed;bottom: 35rpx;width: 700rpx;background-color: #fff;">
				<u-row>
					<u-col>
						<u--text v-if="canSub" :text="'升级可补差价  ' + subprice.toFixed(2) + '元'" color="#ff0000"></u--text>
					</u-col>
				</u-row>
				<u-row>
					<u-col span="4" offset=".5">
						<u--text v-if="chooseVip.sale_price" :text="'金额：' + chooseVip.sale_price" color="#E67B1E"
							size="18">
						</u--text>
						<u--text v-if="chooseVip.sale_price" :text="'类型：' +listT[chooseVip.vip_type].name"
							color="#1E88E6">
						</u--text>
					</u-col>
					<u-col span="4" offset="3">
						<u-button v-if="!chooseVip.sale_price" type="success" text="请选择套餐" @click="topay"></u-button>
						<u-button v-else type="success"
							:text="userInfo.level >= itemI ? '已支付' : userInfo.level < itemI ? '去升级' : '去支付'"
							@click="topay">
						</u-button>
					</u-col>
				</u-row>
			</view>
		</view>
	</view>
</template>

<script>
	var that = this;

	var UserInfo = require('../../../service/UserInfo.js'),
		mUserInfo;
	var apis = require('../../../tools/apis.js');
	var VIP = require('../../../service/ERVIP.js'),
		mVIP;
	var Order = require('../../../service/Order.js'),
		mOrder;
	import dateTool from '../../../tools/date.js';
	export default {
		data() {
			return {
				description: [
					['1.商品【有限制】,录入商品条数500', '2.不可邀请店员,仅限本人使用', '3.无短信提醒功能;', '4.鹰眼视图,总览全店数据;'],
					['1.商品【无限制】,录入商品条数无限制', '2.店员【邀请】，共享全店数据,最多8人同时使用;', '3.有【短信提醒】功能，仅限购买者本人;', '4.鹰眼视图,总览全店数据;'],
					[
						'1.商品【无限制】,录入商品条数无限制；',
						'2.店员【邀请】，共享全店数据,店员数【无限制】;',
						'3.有【短信提醒】功能，仅限购买者本人;',
						'4.鹰眼视图,总览全店数据;',
						'5.可【新建店铺】,最多10个店铺。'
					]
				],
				canSub: false,
				//做一下会员升级
				chooseVip: {},
				subprice: 0,
				itemI: 0,
				listT: [{
						name: '一人一店'
					},
					{
						name: '一人多店'
					}
				],
				package: [],
				userInfo: {},
				isVip: false,
				radiovalue1: 0
			};
		},
		onLoad() {
			that = this;
			mUserInfo = new UserInfo(this.mNetTool);
			mVIP = new VIP(this.mNetTool);
			mOrder = new Order(this.mNetTool);
		},
		onShow() {
			let userInfo = uni.getStorageSync('userInfo');
			let myOrderList = uni.getStorageSync('myOrderList');
			if (myOrderList && myOrderList.rows.length > 0) {
				this.myOrder = myOrderList.rows[0]
			}
			that.userInfo = userInfo;
			console.log(userInfo);
			if (!userInfo) {
				uni.showModal({
					content: '请登录后使用',
					success(r) {
						if (r.confirm) {
							if (that.autoStatus) {
								//支持一键登录
								console.log('进入一键登录');
								that.keyLogin();
							} else {
								uni.navigateTo({
									url: '../login/login'
								});
							}
						} else {}
					}
				});
			} else {
				let nowtime = new Date().getTime();
				let vip_end = new Date(userInfo.vip_end_time).getTime();
				if (nowtime <= vip_end) {
					that.isVip = true;
					uni.setStorageSync('isVip', true);
				} else {
					that.isVip = false;
					uni.setStorageSync('isVip', false);
				}
				console.log(nowtime, vip_end);
			}
		},
		mounted() {
			let userInfo = uni.getStorageSync('userInfo');
			mVIP.list({
				status: 1,
				avue_ui_crud_config: 1
			}, res => {
				console.log('会员', res);
				this.package = res.rows;
				this.chooseVip = res.rows[0];
			});
			that.userInfo = userInfo;
		},
		methods: {
			groupChange(e) {
				console.log(e);
			},
			radioChange(e) {
				console.log(e);

				that.chooseVip = that.package[e];
				console.log(that.userInfo.level, that.chooseVip.vip_type);
				if (that.userInfo.level < that.chooseVip.vip_type) {
					console.log('升级');
					that.canSub = true;
				} else {
					that.canSub = false;
				}
				that.itemI = that.package[e].vip_type;

				// console.log('itemI',that.chooseVip.vip_type)
			},
			click(e) {
				this.itemI = e.index;
				console.log(e);
				let userInfo = uni.getStorageSync('userInfo');
				if (userInfo.level >= 0 && userInfo.level <= 4) {
					that.canSub = true;
					let openid = uni.getStorageSync('openid');
					let end_time = userInfo.vip_end_time;
					let start_time = userInfo.vip_start_time;
					// 会员价格
					let vip_price = userInfo.sale_price;
					// 当前会员的天数
					let days = dateTool.getDaysBetween(start_time, end_time);
					// 使用天数
					let usedDays = dateTool.getDaysBetween(start_time, new Date());
					let subprice = (vip_price / days) * (days - usedDays);
					that.subprice = subprice;
					console.log('天数', days);
					console.log('使用的天数', parseInt(usedDays));
					console.log('价格', vip_price);
					console.log('优惠价格', subprice);
					console.log(that.chooseVip);
				}
			},
			topay() {
				if (!that.chooseVip.sale_price) {
					that.chooseVip = that.package[0];
				}
				let userInfo = uni.getStorageSync('userInfo');
				if (!userInfo) {
					uni.$u.toast('请登录后在使用');
					return;
				}
				// let info = {
				// 	vip_type:that.itemI,
				// 	user_mobile:userInfo.mobile,
				// 	user_id:userInfo.id,
				// 	actual_payment:that.chooseVip.sale_price,

				// }
				let msg;
				let temp;
				let type = '';
				if (that.chooseVip.validity_term_day) {
					msg = that.chooseVip.validity_term_day + '天';
					temp = 1000 * 60 * 60 * 24 * that.chooseVip.validity_term_day;
				} else if (that.chooseVip.validity_time_month) {
					msg = that.chooseVip.validity_time_month + '月';
					temp = 1000 * 60 * 60 * 24 * 31 * that.chooseVip.validity_time_month;
				} else if (that.chooseVip.validity_time_year) {
					msg = that.chooseVip.validity_time_year + '年';
					temp = 1000 * 60 * 60 * 24 * 365 * that.chooseVip.validity_time_year;
				}

				if (that.chooseVip.vip_type_dec) {
					type = that.chooseVip.vip_type_dec;
				}
				let nowtimetemp = new Date().getTime() + temp;
				let year = new Date(nowtimetemp).getFullYear();
				let month = new Date(nowtimetemp).getMonth() + 1;
				let day = new Date(nowtimetemp).getDate();
				console.log('商品过期管家-', year, month, day);

				let openid = uni.getStorageSync('userInfo').open_id;
				// if(that.userInfo.level >= that.itemI ){
				// 	uni.$u.toast('无需重复购买')
				//
				// }
				// 作比较
				console.log(userInfo.level < that.chooseVip.vip_type);
				if (userInfo.level < that.chooseVip.vip_type) {
					//  let end_time = userInfo.vip_end_time
					//  let start_time = userInfo.vip_start_time
					//  // 会员价格
					//  let vip_price = userInfo.sale_price
					// // 当前会员的天数
					//  let days = dateTool.getDaysBetween(start_time,end_time)
					//  // 使用天数
					//  let usedDays = dateTool.getDaysBetween(start_time,new Date() )
					//  let subprice  =( vip_price/days)*(days - usedDays)
					// console.log('天数',days)
					// console.log('使用的天数',parseInt(usedDays))
					// console.log('价格',vip_price)
					// console.log('优惠价格',subprice)
					// console.log(that.chooseVip)
					that.canSub = true;
					let info = {
						order_type: that.chooseVip.vip_type, //套餐类型
						card_id: that.chooseVip.id, //套餐id
						sku_name: '商品过期管家-' + type + '版套餐-时长' + msg,
						real_amount: that.chooseVip.sale_price - that.subprice, //实际价格
						amount: that.chooseVip.scribing_price, //划线价格
						mobile: userInfo.mobile, //手机号
						creater: userInfo.nick_name, //创建者昵称
						user_id: userInfo.id, //用户id
						order_type: that.chooseVip.vip_type, //套餐类型
						end_time: year + '-' + month + '-' + day, //截止时间
						remark: msg //描述
					};
					mOrder.save(info, res => {
						apis.buyWeChatLitOnline(
							res.id,
							that.chooseVip.sale_price,
							openid,
							info.sku_name,
							success => {
								console.log('支付成功');
								uni.showModal({
									content: '恭喜您购买成功！',
									showCancel: false
								});
								// mUserInfo.update({
								// 	id:userInfo.id,
								// 	level:that.chooseVip.vip_type,
								// 	//vip_end_time:'',//失效时间、vip_start_time
								// },res => {

								// 	uni.setStorageSync('userInfo',res.rows[0])
								// })
								console.log(success);
								// mVIP.save({

								// 	vip_type:that.chooseVip.vip_type,
								// 	user_mobile:userInfo.mobile,
								// 	actual_payment:that.chooseVip.sale_price,
								// },res => {
								// 	mUserInfo.update({
								// 		id:userInfo.id,
								// 		level:that.chooseVip.vip_type,
								// 		//vip_end_time:'',//失效时间、vip_start_time
								// 	},res => {

								// 		uni.setStorageSync('userInfo',res.rows[0])
								// 	})
								// })
							},
							fail => {
								console.log('支付失败2');
								//  wx.navigateBack({ delta: 2, })
							}
						);
						// console.log(res);
						return;
					});
				} else {
					that.canSub = false;
					let info = {
						order_type: that.chooseVip.vip_type, //套餐类型
						card_id: that.chooseVip.id, //套餐id
						sku_name: '商品过期管家-' + type + '版套餐-时长' + msg,
						real_amount: that.chooseVip.sale_price, //实际价格
						amount: that.chooseVip.scribing_price, //划线价格
						mobile: userInfo.mobile, //手机号
						creater: userInfo.nick_name, //创建者昵称
						user_id: userInfo.id, //用户id
						order_type: that.chooseVip.vip_type, //套餐类型
						end_time: year + '-' + month + '-' + day, //截止时间
						remark: msg //描述
					};
					mOrder.save(info, res => {
						apis.buyWeChatLitOnline(
							res.id,
							that.chooseVip.sale_price,
							openid,
							info.sku_name,
							success => {
								console.log('支付成功');
								uni.showModal({
									content: '恭喜您购买成功！',
									showCancel: false
								});
							},
							fail => {
								console.log('支付失败2');

							}
						);
						// console.log(res);
						return;
					});
				}
			}
		}
	};
</script>

<style></style>
